import { defineConfig } from 'vite'
import path from 'path'
import vue from '@vitejs/plugin-vue'

// https://vite.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
      // https://cn.vitejs.dev/config/#resolve-alias
      alias: {
        // 设置路径
        '~': path.resolve(__dirname, './'),
        // 设置别名
        '@': path.resolve(__dirname, './src')
      },
      // https://cn.vitejs.dev/config/#resolve-extensions
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    build: {
      target: 'es2020',
      rollupOptions: {
        input: './index.html',
        _output: {//不配置
          // 入口文件
          entryFileNames(chunk) {
            return 'assets/js/app-[hash].js';
          },
          // async-chunk
          chunkFileNames() {
            return 'assets/js/[name]-[hash].js';
          },
          // 资源文件
          assetFileNames(chunk) {
            // css
            if (chunk.name?.endsWith('.css')) {
              return 'assets/css/[name]-[hash].[ext]';
            }
            // image
            if (/.(png|jpg|gif|jpeg|webp|svg)$/.test(chunk.name || '')) {
              return 'assets/imgs/[name].[ext]';
            }

            return `assets/other/[name].[ext]`;
          },
        },
      },
    },
    optimizeDeps: {
      esbuildOptions: {
        target: 'es2020',
      },
    },
})
